package com.easy.controller;

import com.easy.bean.Order;
import com.easy.bean.PrivateClass;
import com.easy.common.CommonResult;
import com.easy.service.IPrivateService;
import com.easy.utils.Page;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PrivateController {

    @Autowired
    IPrivateService privateS;


    @GetMapping("privateclass")
    public CommonResult getPrivateClass(PrivateClass privateClass, Page page){
        List<PrivateClass> list = privateS.getPrivateClass(privateClass,page);
        int count = privateS.getPrivateClassCount(privateClass);
        HashMap map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return CommonResult.success(map);
    }

    @DeleteMapping("privateclass")
    public CommonResult delPrivateClass(@RequestBody PrivateClass privateClass){
        int res = privateS.delPrivateClass(privateClass);
        return CommonResult.success(res);
    }

    @PutMapping("privateclass")
    public CommonResult editPrivateClass(@RequestBody PrivateClass privateClass){
        int res = privateS.editPrivateClass(privateClass);
        privateClass = privateS.getPrivateClassByID(privateClass);
        return CommonResult.success(privateClass);
    }

    @PostMapping("book")
    public CommonResult bookPublicClass(@RequestBody PrivateClass privateClass){
        PrivateClass obj = privateS.getPublicClassByID(privateClass);
        if(obj == null){
            int res = privateS.bookPublicClass(privateClass);
            return CommonResult.success(res);
        }else {
            return CommonResult.fail(400,"不能重复预约");
        }
    }

    @GetMapping("book/{memberID}")
    public CommonResult getBookingByMemberID(@PathVariable int memberID){
        List<PrivateClass> list = privateS.getBookingByMemberID(memberID);
        return CommonResult.success(list);
    }

    @DeleteMapping("book/{id}")
    public CommonResult delBooking(@PathVariable int id){
        int res = privateS.delBooking(id);
        return CommonResult.success(res);
    }

    @GetMapping("memberbook/{courseID}")
    public CommonResult getBookingByCourseID(@PathVariable int courseID){
        List<PrivateClass> list = privateS.getBookingByCourseID(courseID);
        return CommonResult.success(list);
    }

    @GetMapping("memberbuy/{memberID}")
    public CommonResult getPrivateClassByMemberID(@PathVariable int memberID){
        List<PrivateClass> list = privateS.getPrivateClassByMemberID(memberID);
        return CommonResult.success(list);
    }

    @GetMapping("bookchart")
    public CommonResult bookChart(){
        List<PrivateClass> list = privateS.bookChart();
        List<PieDto> pieList = new ArrayList<>();
        System.out.println(list);
        //循环
        for(PrivateClass p : list){
            pieList.add(new PieDto(p.getCourseName(),p.getTotal()));
        }
        return CommonResult.success(pieList);
    }
}

@Data
@AllArgsConstructor
class PieDto {
    private String name;
    private int value;
}



